package com.kye.express.business.waybill_take_photo.yunmai;

import android.content.Context;
import android.text.TextUtils;

import com.kye.express.R;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.RxView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.online.GetPayResultResultBean;
import com.kye.model.bean.online.request.RequestGetPayResultBean;
import com.kye.model.bean.user.User;
import com.kye.model.bean.waybill_take_photo.RequestValidateWaybillUploadBean;
import com.kye.model.bean.waybill_take_photo.ValidateWaybillUploadResultBean;
import com.kye.model.service.OnlinePayService;
import com.kye.model.service.WaybillTakePhotoService;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public interface CameraActivityContract {

    interface CameraActivityView extends RxView {

        void onComplete();

        void onLoading();

        void onLoaded(OpenApiResult<ValidateWaybillUploadResultBean> validateWaybillUploadResultBean);

        void onApiError(String msg);

        void onOtherError(String msg);
    }

    class CameraActivityViewPresenter extends BasePresenter<CameraActivityContract.CameraActivityView> {

        private Context mContext;

        public CameraActivityViewPresenter(Context context) {
            this.mContext = context;
        }

        public void validateWaybillUpload(String waybill) {
            String phone = "";
            User user = UserSession.getInstance().getUser();
            if (user != null) {
                phone = user.getPhone();
            }

            RetrofitManager.getInstance().createOpenApiService(WaybillTakePhotoService.class)
                    .validateWaybillUpload(new RequestValidateWaybillUploadBean(phone, waybill))
                    .compose(getView().<OpenApiResult<ValidateWaybillUploadResultBean>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<ValidateWaybillUploadResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onOtherError(mContext.getString(R.string.common_net_error));
                        }

                        @Override
                        public void onLoaded() {
                            if (getView() != null) {
                                getView().onComplete();
                            }
                        }

                        @Override
                        public void onSuccess(OpenApiResult<ValidateWaybillUploadResultBean> result, ValidateWaybillUploadResultBean datas) {
                            if (result != null) {
                                getView().onLoaded(result);
                            } else {
                                getView().onOtherError(mContext.getString(R.string.common_net_error));
                            }
                        }

                        @Override
                        public void onApiError(OpenApiResult<ValidateWaybillUploadResultBean> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onApiError(msg);
                            } else {
                                getView().onOtherError(mContext.getString(R.string.common_net_error));
                            }
                        }

                    });
        }
    }
}
